import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter_udid/flutter_udid.dart';
import 'package:permission_handler/permission_handler.dart';

enum YKFlutterConfigPlatform {
  none,
  iOS,
  android,
  linux,
  MacOS,
  Windows
}

class YKFlutterConfig {

  static YKFlutterConfig? _instance;
  YKFlutterConfigPlatform? _platform;
  String _sdkVersion = "0.0.0";
  String _deviceId = "";
  bool? _photoIsGrad = null;
  bool? _micIsGrad = null;

  YKFlutterConfigPlatform get platform => _platform!;

  String get sdkVersion => _sdkVersion;

  String get deviceId => _deviceId;

  bool? get photoIsGrad => _photoIsGrad;

  bool? get micIsGrad => _micIsGrad;

  factory YKFlutterConfig.getInstance() {
    _instance ??= YKFlutterConfig._();
    return _instance!;
  }

  YKFlutterConfig._();

  setup({Function()? finishCallBack}) async {
    await _getPlatform();
    await _getUUid();
    if (finishCallBack != null) {
      finishCallBack!();
    }
  }

  _getPlatform() async {
    if (Platform.isAndroid) {
      _platform = YKFlutterConfigPlatform.android;
      AndroidDeviceInfo info = (await DeviceInfoPlugin().androidInfo);
      _sdkVersion = "${info.version.sdkInt}";
      _deviceId = "${info.device}";
    } else if (Platform.isIOS) {
      _platform = YKFlutterConfigPlatform.iOS;
      IosDeviceInfo info = (await DeviceInfoPlugin().iosInfo);
      _sdkVersion = "${info.systemVersion}";
    } else if (Platform.isLinux) {
      _platform = YKFlutterConfigPlatform.linux;
      LinuxDeviceInfo info = (await DeviceInfoPlugin().linuxInfo);
      _sdkVersion = "${info.prettyName}";
    } else if (Platform.isMacOS) {
      _platform = YKFlutterConfigPlatform.MacOS;
      MacOsDeviceInfo info = (await DeviceInfoPlugin().macOsInfo);
      _sdkVersion = "${info.majorVersion}.${info.minorVersion}.${info.patchVersion}";
    } else if (Platform.isWindows) {
      _platform = YKFlutterConfigPlatform.Windows;
      WindowsDeviceInfo info = (await DeviceInfoPlugin().windowsInfo);
      _sdkVersion = "${info.productName}.${info.releaseId}";
    } else {
      _platform = YKFlutterConfigPlatform.none;
      WebBrowserInfo info = (await DeviceInfoPlugin().webBrowserInfo);
      _sdkVersion = "${info.appVersion ?? ""}";
    }
  }

  _getUUid() async {
    _deviceId = await FlutterUdid.udid;
  }

  Future<bool> checkPhotoPermison(Function? callback) async {
    var isGrant = false;
    Permission permission = Permission.photos;
    if (Platform.isAndroid) {
      var sdkVersion = await DeviceInfoPlugin().androidInfo.then((value) => value.version).then((value) => value.sdkInt);
      if (sdkVersion < 33) {
        permission = Permission.storage;
      }
    }
    PermissionStatus status = await permission.status;

    if (status.isGranted || status.isLimited) {
      isGrant = true;
    } else {
      PermissionStatus status = await permission.request();
      if (status.isPermanentlyDenied || status.isDenied) {
        openAppSettings();
        isGrant = false;
      } else if (status.isGranted || status.isLimited) {
        isGrant = (status == PermissionStatus.granted);
      }
    }
    _photoIsGrad = isGrant;
    return isGrant;
  }


  Future<bool> checkMicPhotoPermison(Function? callback) async {
    var isGrant = false;
    Permission permission = Permission.microphone;
    PermissionStatus status = await permission.status;

    if (status.isGranted || status.isLimited) {
      isGrant = true;
    } else {
      PermissionStatus status = await permission.request();
      if (status.isPermanentlyDenied || status.isDenied) {
        openAppSettings();
        isGrant = false;
      } else if (status.isGranted || status.isLimited) {
        isGrant = (status == PermissionStatus.granted);
      }
    }
    _micIsGrad = isGrant;
    return isGrant;
  }
}